package com.yongchao.role.service.Serviceimp;

import com.yongchao.role.bean.RoleBean;
import com.yongchao.role.dao.RoleDao;
import com.yongchao.role.service.YYC_RoleService;
import com.yongchao.utils.YinDaoFactory;

import java.util.ArrayList;
import java.util.List;

public class YYC_RoleServiceImp implements YYC_RoleService {

    private RoleDao roleDao;


    @Override
    public Integer updateRoleById(Integer id, String name) {
        YinDaoFactory<RoleDao> RoleDaoFactory = new YinDaoFactory();
        roleDao=RoleDaoFactory.getDaoTransaction(RoleDao.class);
        Integer userCount = roleDao.findUserCountByRoleId(id);
        if (userCount==0){
            roleDao.updateRoleById(id, name);
        }else {
            return 0;
        }
        return roleDao.updateRoleById(id, name);
    }




    @Override
    public Integer deleteRoleById(Integer id) {
        YinDaoFactory<RoleDao> RoleDaoFactory = new YinDaoFactory();
        roleDao=RoleDaoFactory.getDaoTransaction(RoleDao.class);
        roleDao.deleteRoleById(id);
        return 1;
    }

    @Override
    public List<RoleBean> checkRole() {

        YinDaoFactory<RoleDao> RoleDaoFactory = new YinDaoFactory();
        roleDao=RoleDaoFactory.getDao(RoleDao.class);
        return roleDao.checkRole();
    }



    @Override
    public List<RoleBean> findRoleByPhone(String phone) {
        YinDaoFactory<RoleDao> RoleDaoFactory = new YinDaoFactory();
        roleDao=RoleDaoFactory.getDao(RoleDao.class);
        List<String> role = roleDao.findRoleByPhone(phone);
        List<RoleBean> RoleList = new ArrayList();

        for(int index=0;index<role.size();index++){
            String roleBean = role.get(index);
            if ("校级考勤管理员".equals(roleBean)){
                RoleList = roleDao.checkRole();
                System.out.println(RoleList);
            }
        }
        return RoleList;
    }

    @Override
    public Integer findUserCountByRoleId(Integer id) {
        YinDaoFactory<RoleDao> RoleDaoFactory = new YinDaoFactory();
        roleDao=RoleDaoFactory.getDao(RoleDao.class);
        return  roleDao.findUserCountByRoleId(id);
    }



    @Override
    public Integer addRole(String name) {
        YinDaoFactory<RoleDao> RoleDaoFactory = new YinDaoFactory();
        roleDao = RoleDaoFactory.getDaoTransaction(RoleDao.class);
        Integer roleCount = roleDao.findRoleCountByRoleName(name);
        if (roleCount!=0){
            return 0;
        }




        return roleDao.addRole(name);
    }

    @Override
    public Integer findRoleCountByRoleName(String name) {
        YinDaoFactory<RoleDao> RoleDaoFactory = new YinDaoFactory();
        roleDao = RoleDaoFactory.getDaoTransaction(RoleDao.class);
        return roleDao.findRoleCountByRoleName(name);
    }



}
